package alibaba

import (
	"context"
	"encoding/json"
	"fmt"
	dysmsapi "github.com/alibabacloud-go/dysmsapi-20170525/v4/client"
	"github.com/alibabacloud-go/tea/tea"
)

type Service struct {
	//签名
	SignName string
	client   *dysmsapi.Client
}

func NewService(signName string, client *dysmsapi.Client) *Service {
	return &Service{
		SignName: signName,
		client:   client,
	}
}

func (s *Service) Send(ctx context.Context, tqlId string, args map[string]string, number string) error {
	request := &dysmsapi.SendSmsRequest{}

	// 需替换成为您的短信模板code
	request.TemplateCode = tea.String(tqlId)
	// 示例值：{\"code\":\"1234\"}
	val, _ := json.Marshal(args)
	request.TemplateParam = tea.String(string(val))
	// 需替换成为您的接收手机号码
	request.PhoneNumbers = tea.String(number)
	// 需替换成为您的短信签名
	request.SignName = tea.String(s.SignName)

	_, err := s.client.SendSms(request)
	if err != nil {
		fmt.Println(err)
		return err
	}

	return nil
}
